---
title: "Balancing algorithm report"
output:
  html_document:
    df_print: paged
editor_options:
  chunk_output_type: console
---

```{r,message=FALSE,echo=FALSE}
library(dplyr)
```

```{r,echo=FALSE,message=FALSE,warning=FALSE}
df <- read.csv("slovenia_clean.csv")

est <- read.csv("Sloveniaestimates.csv")
est$est=est$est/100
est$Freq=nrow(df) * est$est
est$est=NULL
X <- split(est, est$variable)
X=lapply(X, function(x) { x["variable"] <- NULL; x })
list2env(X ,.GlobalEnv)
joint.d=rename(joint, joint=label)
pol.d=rename(pol, pol=label)
sex.d=rename(sex, sex=label)
vot.d=rename(vot, vot=label)

library(survey)
df$ids=seq(1:nrow(df))
df1 <- svydesign(ids=~1, data=df)

df.r <- rake(design = df1,
             sample.margins = list(~joint, ~pol, ~sex, ~vot),
             population.margins = list(joint.d, pol.d, sex.d, vot.d))

df.r1 <- trimWeights(df.r, lower=0.6, upper=7,
                                   strict=TRUE) 
df$w=weights(df.r1)
df$Freq=round(df$w,0)

df0=select(df,Record.ID,joint,pol,sex,vot,Freq)
MyData <- df0[rep(row.names(df0), df0$Freq), 1:5]
write.csv(MyData,"MyData.csv",row.names=F)

```

# Python (config = code) 
```{python engine.path="/usr/bin/python3.5",results='hide'}
import sys
sys.path.append('/home/shared/data/balancing/')
import drop
config = "{1: {'degreeHigh':9.5,'noDegreeHigh':39.6,'degreeLow':16.6,'noDegreeLow':34.3}},{2: {'interest':53.0,'LowInterest':47.0}},{3: {'Male':48.7,'Female':51.3}},{4: {'SDS':26.11,'Nsi':17.50,'Ver':10.88,'SD':8.51,'PS':6.99,'ZL':5.76,'SNS':4.25,'Undec':20.00}}"

filename = '/home/fernando/Misc/bjpolFinal/Slovenia/MyData.csv'

drop.run(config, filename, "/home/fernando/Misc/bjpolFinal/Slovenia/myBal", 2.5)
```

